/*
 * @lc app=leetcode.cn id=224 lang=cpp
 *
 * [224] 基本计算器
 */
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
// @lc code=start
class Solution
{
    int is_flag(const char ch)
    {
        if (ch == '+') {
            return 1;
        }
        if (ch == '-') {
            return -1;
        }
        return 0;
    }

    int string2number(const string str)
    {
        int result = 0;
        for (auto ch : str) {
            result += result * 10 + ch - '0';
        }
        return result;
    }
    string op_number(const string op, const string number)
    {
        int ret = string2number(number);
        string new_number;
        while (ret) {
            new_number.insert(0, 1, char((ret % 10) - '0'));
            ret /= 10;
        }
        cout << "new_number: " << new_number;
        return new_number;
    }

  public:
    int calculate(string s)
    {
        vector<string> computer_str;
        stack<string> result;
        string number;

        for (auto ch : s) {
            if (ch == '(' || ch == ')' || ch == '-' || ch == '+') {
                if (!number.empty()) {
                    computer_str.push_back(number);
                    number.clear();
                }

                string brace;
                brace.push_back(ch);
                computer_str.push_back(brace);
            } else if (ch == ' ') {
                continue;
            } else {
                number.push_back(ch);
            }
        }
        // for (auto str : computer_str) {
        //     cout << str;
        // }

        for (auto num : computer_str) {
            if (num == "(") {
                result.push(num);
            } else if (num == ")") {
                while (!result.empty() && result.top() != "(") {
                    string num = 0;
                    string op;

                    num = result.top();
                    result.pop();

                    op = result.top();
                    result.pop();

                    result.push(op_number(op, num));
                }
                if (result.top() == "(") {
                    result.pop();
                }
            } else if (num == "+" || num == "-") {
                result.push(num);
            } else {
                if (result.top() == "-" || result.top() == "+") {
                    string num;
                    string op = result.top();
                    result.pop();
                    num = result.top();
                    result.pop();
                    result.push(op_number(op, num));
                }
            }
        }
        return 0;
    }
};
// @lc code=end
int main()
{
    Solution sol;
    string str("(1+(4+5+2)-3)+(6+8)");
    sol.calculate(str);
}
